package com.flag.liutao.modules.flag.controller;


import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.IdsParam;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.modules.flag.param.UnitParam;
import com.flag.liutao.modules.flag.param.UnitQueryParam;
import com.flag.liutao.modules.flag.service.IUnitService;
import com.flag.liutao.modules.flag.vo.UnitDTO;
import com.flag.liutao.modules.flag.vo.UnitVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-12-12
 */
@RestController
@RequestMapping("/admin/unit")
@Slf4j
@RequiredArgsConstructor
@Api(tags = "分队管理")
public class UnitController {
    private final IUnitService unitService;

    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys_power:unit_R')")
    @ApiOperation(value = "分页获取分队信息",notes = "分队管理：分页获取分队信息")
    public ResponseResult<Map<String,Object>> getUnitPage(UnitQueryParam param){
        Map<String,Object> map = unitService.getUnitPage(param.getPage(),param.getLimit(),param.getKeyword());
        return ResponseResult.ok(map);
    }

    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys_power:unit_R')")
    @ApiOperation(value = "获取分队列表",notes = "分队管理：获取分队列表")
    public ResponseResult<List<UnitVo>> getUnitList(){
        List<UnitVo> list  = unitService.getUnitList();
        return ResponseResult.ok(list);
    }

    @GetMapping("userList")
    @PreAuthorize("hasAuthority('sys_power:unit_R')")
    @ApiOperation(value = "获取分队下所有队员列表",notes = "分队管理：获取分队下所有队员列表")
    public ResponseResult<List<UnitDTO>> getUnitUserList(){
        List<UnitDTO> list  = unitService.getUnitUserList();
        return ResponseResult.ok(list);
    }

    @GetMapping("add")
    @PreAuthorize("hasAuthority('sys_power:unit_C')")
    @ApiOperation(value = "添加分队信息",notes = "分队管理：添加分队信息")
    public ResponseResult<Boolean> addUnit(@Validated @RequestBody UnitParam param){
        unitService.addUnit(param);
        return ResponseResult.ok();
    }

    @GetMapping("edit")
    @PreAuthorize("hasAuthority('sys_power:unit_U')")
    @ApiOperation(value = "修改分队信息",notes = "分队管理：修改分队信息")
    public ResponseResult<Boolean> editUnit(@Validated @RequestBody UnitParam param){
        unitService.editUnit(param);
        return ResponseResult.ok();
    }

    @GetMapping("del")
    @PreAuthorize("hasAuthority('sys_power:unit_D')")
    @ApiOperation(value = "删除分队信息",notes = "分队管理：删除分队信息")
    public ResponseResult<Boolean> delUnit(@Validated @RequestBody IdParam param){
        unitService.delUnit(param.getId());
        return ResponseResult.ok();
    }

    @GetMapping("dels")
    @PreAuthorize("hasAuthority('sys_power:unit_D')")
    @ApiOperation(value = "批量删除分队信息",notes = "分队管理：批量删除分队信息")
    public ResponseResult<Boolean> delsUnit(@Validated @RequestBody IdsParam param){
        for (Long id : param.getIds()) {
            unitService.delUnit(id);
        }
        return ResponseResult.ok();
    }




}
